package com.zhike.service.im.client.core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.springframework.beans.factory.annotation.Autowired;

import com.zhike.service.im.client.ClientCoreSDK;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AutoReLoginDaemon {

	private final static String TAG = AutoReLoginDaemon.class.getSimpleName();
	private static AutoReLoginDaemon instance = null;
	public static int AUTO_RE$LOGIN_INTERVAL = 3000;// 2000;

	private boolean autoReLoginRunning = false;
	private boolean _excuting = false;
	private Timer timer = null;

	public static AutoReLoginDaemon getInstance() {
		if (instance == null) {
			synchronized (AutoReLoginDaemon.class) {
				if (instance == null) {
					instance = new AutoReLoginDaemon();
				}
			}
		}
		return instance;
	}

	private AutoReLoginDaemon() {
		init();
	}

	private void init() {
		timer = new Timer(AUTO_RE$LOGIN_INTERVAL, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				run();
			}
		});
	}

	public void run() {
		if (!_excuting) {
			_excuting = true;
			if (ClientCoreSDK.DEBUG)
				log.debug("TAG - {}", "【IMCORE-TCP】自动重新登陆线程执行中, autoReLogin?" + ClientCoreSDK.autoReLogin + "...");
			int code = -1;
			if (ClientCoreSDK.autoReLogin) {
				LocalSocketProvider.getInstance().closeLocalSocket();
				code = LocalDataSender.getInstance().sendLogin(ClientCoreSDK.getInstance().getCurrentLoginInfo());
			}

			if (code == 0) {
				// LocalUDPDataReciever.getInstance().startup();
			}

			_excuting = false;
		}
	}

	public void stop() {
		if (timer != null)
			timer.stop();

		autoReLoginRunning = false;
	}

	public void start(boolean immediately) {
		stop();
		if (immediately)
			timer.setInitialDelay(0);
		else
			timer.setInitialDelay(AUTO_RE$LOGIN_INTERVAL);
		timer.start();
		autoReLoginRunning = true;
	}

	public boolean isautoReLoginRunning() {
		return autoReLoginRunning;
	}
}
